package com.ruoyi.quartz.task;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bizsys.domain.LoanOrder;
import com.ruoyi.bizsys.service.ILoanOrderService;
import com.ruoyi.business.common.AsyncService;
import com.ruoyi.common.config.EmailConfig;
import com.ruoyi.common.utils.EmailUtils;
import com.ruoyi.common.utils.FeishuUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.vo.EmailVo;
import com.ruoyi.quartz.util.EmailsTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

@Slf4j
@Component("loanOrderTask")
public class LoanOrderTask {

    @Autowired
    private ILoanOrderService iLoanOrderService;

    @Autowired
    private AsyncService asyncService;

    public void checkStatus(){
        QueryWrapper<LoanOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("loan_status",3).or().eq("loan_status",1).select("pay_trans_serial_no","loan_status");
        List<LoanOrder> list = iLoanOrderService.list(queryWrapper);

        String payTransSerialNo_1 = "";
        String payTransSerialNo_3 = "";
        for (int i=0;i<list.size();i++){
            if(StringUtils.isNotBlank(list.get(i).getPayTransSerialNo())){
                if(list.get(i).getLoanStatus()==1){
                    payTransSerialNo_1+=list.get(i).getPayTransSerialNo()+",";
                }else if(list.get(i).getLoanStatus()==3){
                    payTransSerialNo_3+=list.get(i).getPayTransSerialNo()+",";
                }
            }
        }

        //发送飞书
        log.info("payTransSerialNo_1:"+payTransSerialNo_1);
        if(StringUtils.isNotBlank(payTransSerialNo_1) && payTransSerialNo_1.split(",").length>5){
            payTransSerialNo_1 = payTransSerialNo_1.substring(0,payTransSerialNo_1.length()-1);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("payTransSerialNos",payTransSerialNo_1);
            asyncService.sendFeishu("monitor-group","loan_status_monitor",jsonObject);
        }

        //推送邮件
        log.info("payTransSerialNo_3:"+payTransSerialNo_3);
        if(StringUtils.isNotBlank(payTransSerialNo_3) && payTransSerialNo_3.split(",").length>5){
            payTransSerialNo_3 = payTransSerialNo_3.substring(0,payTransSerialNo_3.length()-1);


            String content = "<html><head></head><body>Dear Paystack,<br/><br/>The following transactions from our disbursements status has been pending for a long time, which has seriously affected our business,please deal with it as soon as possible.<br/><br/>%s<br/><br/>Regards</body></html>";
            String format = String.format(content, payTransSerialNo_3);
            EmailVo emailVo = EmailVo.builder()
                    .subject("Payment transactions continue to be pending (Link Credit & Paystack)")
                    .cc(EmailConfig.getLoanCc().split(","))
                    .addressees(EmailConfig.getLoanAddress().split(","))
                    .content(format)
                    .build();
            asyncService.sendCustomizeEmail(emailVo);
        }


    }
}
